﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AxSHDocVw;
using mshtml;

namespace Insurance_Quotation_App
{
    class Autofill
    {
        // Set value for the attribute that has the name (attName)

        public static void SetText(AxWebBrowser webBrowser, string attribute, string attName, string value)
        {
            // Get a collection of all the tags with name "input";

            IHTMLElementCollection tagsCollection = ((IHTMLDocument3)(webBrowser.Document)).getElementsByTagName("input");

            foreach (IHTMLElement currentTag in tagsCollection)
            {

                // If the attribute of the current tag has the name attName

                if (currentTag.getAttribute(attribute).Equals(attName))//gets the name of the htmlElement and compares to attName
                {
                    // Then set its attribute "value".
                    currentTag.setAttribute("value", value);
                }
            }
        }

        public static void SetRadio(AxWebBrowser webBrowser, string attribute, string attName, string value)
        {
            // Get a collection of all the tags with name "input";

            IHTMLElementCollection tagsCollection = ((IHTMLDocument3)(webBrowser.Document)).getElementsByTagName("input");

            foreach (IHTMLElement currentTag in tagsCollection)
            {

                // If the attribute of the current tag has the name attName
                if (currentTag.getAttribute(attribute).Equals(attName))     //gets the name of the htmlElement and compares to attName
                {
                    // Then set its attribute "value".
                    currentTag.setAttribute("value", value);
                    currentTag.setAttribute("checked", "checked");
                }
            }
        }

        public static void SetSelectList(AxWebBrowser webBrowser, string attribute, string attName, string value)
        {
            // Get a collection of all the tags with name "input";

            IHTMLElementCollection tagsCollection = ((IHTMLDocument3)(webBrowser.Document)).getElementsByTagName("select");

            foreach (IHTMLElement currentTag in tagsCollection)
            {

                // If the attribute of the current tag has the name attName

                if (currentTag.getAttribute(attribute).Equals(attName))
                {//gets the name of the htmlElement and compares to attName
                    //HtmlElementCollection optionsCollection = currentTag.Children;

                    //foreach (HtmlElement currentOption in optionsCollection)
                    //{
                    //    //Console.WriteLine(currentOption.TagName);
                    //    if (currentOption.InnerText == value)
                    //    {
                    //        currentOption.SetAttribute("value", value);
                    //    }

                    //}
                    currentTag.setAttribute("value", value);
                }
            }
        }

        public static void SetSelectList(AxWebBrowser webBrowser, string attribute, string attName, string value, bool requireWait)
        {
            // Get a collection of all the tags with name "input";

            IHTMLElementCollection tagsCollection = ((IHTMLDocument3)(webBrowser.Document)).getElementsByTagName("select");

            foreach (IHTMLElement currentTag in tagsCollection)
            {
                if (requireWait)
                {
                    ((HTMLSelectElement)(currentTag)).FireEvent("onchange");
                }

                if (currentTag.getAttribute(attribute).Equals(attName))
                {
                    currentTag.setAttribute("value", value);
                }
            }
        }

        public static void ClickButton(AxWebBrowser webBrowser, string attribute, string attName, string tagname)
        {
            IHTMLElementCollection col = ((IHTMLDocument3)(webBrowser.Document)).getElementsByTagName(tagname);

            foreach (IHTMLElement element in col)
            {
                if (element.getAttribute(attribute) != null)
                {
                    if (element.getAttribute(attribute).Equals(attName))
                    {
                        // Invoke the "Click" member of the button
                        element.click();
                    }
                }
            }
        }
        #region Regular WebBrowser
        public static void SetText(WebBrowser webBrowser, string attribute, string attName, string value)
        {
            // Get a collection of all the tags with name "input";

            HtmlElementCollection tagsCollection = webBrowser.Document.GetElementsByTagName("input");

            foreach (HtmlElement currentTag in tagsCollection)
            {

                // If the attribute of the current tag has the name attName

                if (currentTag.GetAttribute(attribute).Equals(attName))//gets the name of the htmlElement and compares to attName
                {
                    // Then set its attribute "value".
                    currentTag.SetAttribute("value", value);
                }
            }
        }

        public static void SetRadio(WebBrowser webBrowser, string attribute, string attName, string value)
        {
            // Get a collection of all the tags with name "input";

            HtmlElementCollection tagsCollection = webBrowser.Document.GetElementsByTagName("input");

            foreach (HtmlElement currentTag in tagsCollection)
            {

                // If the attribute of the current tag has the name attName
                if (currentTag.GetAttribute(attribute).Equals(attName))     //gets the name of the htmlElement and compares to attName
                {
                    // Then set its attribute "value".
                    currentTag.SetAttribute("value", value);
                    currentTag.SetAttribute("checked", "checked");
                }
            }
        }

        public static void SetSelectList(WebBrowser webBrowser, string attribute, string attName, string value, bool requireWait)
        {
            // Get a collection of all the tags with name "input";

            HtmlElementCollection tagsCollection = webBrowser.Document.GetElementsByTagName("select");

            foreach (HtmlElement currentTag in tagsCollection)
            {
                if (requireWait)
                {
                    currentTag.RaiseEvent("onchange");
                }

                if (currentTag.GetAttribute(attribute).Equals(attName))
                {
                    currentTag.SetAttribute("value", value);
                }
            }
        }

       
        public static void ClickButton(WebBrowser webBrowser, string attribute, string attName, string tagname)
        {
            HtmlElementCollection col = webBrowser.Document.GetElementsByTagName(tagname);

            foreach (HtmlElement element in col)
            {
                if (element.GetAttribute(attribute) != null)
                {
                    if (element.GetAttribute(attribute).Equals(attName))
                    {
                        // Invoke the "Click" member of the button
                        element.InvokeMember("Click");
                    }
                }
            }
        }
        #endregion
    }
}
